package server.content.skills.fishing;

import core.util.Misc;
 
import server.content.skilling.PlayerSkill;
import server.event.CycleEvent;
import server.event.CycleEventContainer;
import server.event.CycleEventHandler;
import server.game.players.Client;

public class MHarpoonFishing extends PlayerSkill {

	private final int TUNA = 359;
	private final int SHARK = 383;

	public MHarpoonFishing(Client c, int skillId) {
		super(c, skillId);
		setTick(2);
	}

	@Override
	public void init() {
		kill();
		if (getClient().getItems().freeSlots() < 1) {
			kill();
			getClient().sendMessage(
					"Your inventory is too full to catch anymore fish.");
			return;
		}
		if (getPlayerLevel() <= 34) {
			kill();
			getClient().sendMessage(
					"You need a fishing level of atleast 35 to fish here.");
			return;
		}
		if (getClient().getItems().playerHasItem(311)) {
			if (startTime < 0)
				startTime = System.currentTimeMillis();
			doAnimation(618);
			setActive(true);
			process();
		} else {
			getClient().sendMessage("You need a Harpoon to fish here.");
			kill();
			return;
		}
	}

	@Override
	public void process() {
		CycleEventHandler.getSingleton().addEvent(getClient(),
				new CycleEvent() {

					@Override
					public void execute(CycleEventContainer container) {
						if (isActive()) {
							if (getClient().getItems().freeSlots() < 1) {
								kill();
								getClient()
										.sendMessage(
												"Your inventory is too full to catch anymore fish.");
								return;
							}
							doAnimation(618);
							if (getPlayerLevel() >= 76) {
								if (Misc.random(100) <= getChanceForLevel()) {
									final int randFish = Misc.random(15);
									if (randFish == 1 || randFish == 5
											|| randFish == 8) {
										addItem(SHARK, 1);
										addXP(330, getSkillId());
										getClient().sendMessage(
												"You manage to catch a Shark.");
									} else {
										addItem(TUNA, 1);
										addXP(210, getSkillId());
										getClient().sendMessage(
												"You manage to catch a Tuna.");
									}
								}
							} else if (getPlayerLevel() >= 35
									&& getPlayerLevel() <= 75) {
								if (Misc.random(100) <= getChanceForLevel()) {
									addItem(TUNA, 1);
									addXP(210, getSkillId());
									getClient().sendMessage(
											"You manage to catch a Tuna.");
								}
							}
						} else {
							container.stop();
						}
					}

					@Override
					public void stop() {

					}
				}, getTick());
	}

	@Override
	public void kill() {
		setTick(2);
		setActive(false);
		resetAnim();
	}

	public int getChanceForLevel() {
		final int lvl = getPlayerLevel();
		if (lvl > 34 && lvl < 51) {
			return 20;
		} else if (lvl > 50 && lvl < 61) {
			return 30;
		} else if (lvl > 60 && lvl < 71) {
			return 40;
		} else if (lvl > 70 && lvl < 81) {
			return 50;
		} else if (lvl > 80 && lvl < 99) {
			return 55;
		}
		return 10;
	}

}
